package com.zzw.controller;

import com.zzw.pojo.MyResponse;
import com.zzw.pojo.User;
import com.zzw.service.IUserService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * 用户controller
 *
 * @Author: zhangzhiwei
 * @Date: 2022/4/6 22:58
 */
@RestController
@RequestMapping("/user")
public class UserController {

    private static final Logger log = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private IUserService userService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 用户注册
     * @param email 邮箱
     * @param password 密码
     * @param code 验证码
     * @return true-注册成功；false-注册失败
     */
    @CrossOrigin
    @GetMapping("/register/{email}/{password}/{code}")
    public MyResponse<Object> register(@PathVariable String email, @PathVariable String password, @PathVariable String code){

        MyResponse<Object> myResponse = new MyResponse<>();
        try {
            // 判断验证码是否正确
            String e = stringRedisTemplate.opsForValue().get(email);
            if(StringUtils.isBlank(e)){
                myResponse.setCode(500);
                myResponse.setMsg("请点击获取验证码");
                return myResponse;
            } else if (!e.equals(code)) {
                myResponse.setCode(500);
                myResponse.setMsg("验证码不正确");
                return myResponse;
            }
            // 判断该用户是否注册过
            boolean isRegister = userService.isRegister(email);
            if(isRegister){
                myResponse.setCode(500);
                myResponse.setMsg("该用户已注册");
                return myResponse;
            }
            // 封装User, 并注册
            User user = new User(email, password);
            int result = userService.register(user);
            myResponse.setCode(200);
            if(result > 0){
                myResponse.setMsg("用户注册成功");
            } else {
                myResponse.setMsg("用户注册失败");
            }
            return myResponse;
        } catch (Exception e) {
            log.error("用户注册异常", e);
            myResponse.setCode(500);
            myResponse.setMsg("用户注册异常");
            return myResponse;
        }
    }

    @CrossOrigin
    @GetMapping("/login/{email}/{password}")
    public MyResponse<String> login(HttpServletResponse response, @PathVariable String email, @PathVariable String password){

        MyResponse<String> myResponse = new MyResponse<>();
        try {
            String s = userService.login(email, password);
            if(null == s){
                myResponse.setCode(500);
                myResponse.setMsg("用户不存在或者密码错误");
            } else {
                myResponse.setCode(200);
                myResponse.setMsg("登录成功");
                myResponse.setData(s);
                // 生成token
                String token = UUID.randomUUID().toString();
                stringRedisTemplate.opsForValue().set(token, email, 30 * 60, TimeUnit.SECONDS);
                // 将token返回浏览器
                Cookie cookie = new Cookie("token", token);
                cookie.setPath("/");
                response.addCookie(cookie);
            }
            return myResponse;
        } catch (Exception e) {
            log.error("用户登录异常", e);
            myResponse.setCode(500);
            myResponse.setMsg("用户登录异常");
            return myResponse;
        }
    }

}
